<?
    final class Principio_Model_TypeInCompanyMapper extends Principio_Mapper
    {
        protected static $db_table = null;


        public static function fetchByModelCompany(Principio_Model_Company_Abstract $model_company_abstract)
        {
            if ($model_company_abstract->getID() == -1)
                return Principio_Model_TypeInCompany::makeCollection();

            try
            {
                // TODO - add distinct and get only ID
                $select = Principio_Model_TypeMapper::getDbTable()
                    ->select()
                    ->where(
                    "id IN (?)",
                    Principio_Model_SpaMapper::getDBTable()
                        ->select()
                        ->distinct()
                        ->from(Principio_Model_SpaMapper::getDBTable(), "type_id")
                        ->where("company_id=?", $model_company_abstract->getID())
                );

                $types_raw = Principio_Model_TypeMapper::getDBTable()->fetchAll($select);

                return Principio_Model_TypeInCompany::makeCollection([
                    'model_company_abstract' => $model_company_abstract,
                    'values' => $types_raw
                ]);
            }
            catch (Exception $ex)
            {
                return self::reportException($ex);
            };
        }


        public static function fetchByModelType(Principio_Model_Type $model_type)
        {
            try
            {
                // TODO - add distinct and get only ID
                $select = Principio_Model_Company_AbstractMapper::getDbTable()
                    ->select()
                    ->where(
                    "id IN (?)",
                    Principio_Model_SpaMapper::getDBTable()
                        ->select()
                        ->distinct()
                        ->from(Principio_Model_SpaMapper::getDBTable(), "company_id")
                        ->where("type_id=?", $model_type->getID())
                );

                $companies_raw = Principio_Model_Company_AbstractMapper::getDBTable()->fetchAll($select);

                return Principio_Model_TypeInCompany::makeCollection([
                    'model_type' => $model_type,
                    'values' => $companies_raw
                ]);
            }
            catch (Exception $ex)
            {
                return self::reportException($ex);
            };
        }
    }
?>